package net.zretc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import net.zretc.entity.Order;
import net.zretc.mapper.OrderMapper;
import net.zretc.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public List<Order> findAll() {
        try {
            return orderMapper.selectList(null);
        } catch (Exception e) {
            System.err.println("查询所有订单出错: " + e.getMessage());
            return null;
        }
    }

    @Override
    public List<Order> findByUserId(Integer userId) {
        try {
            QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id", userId);
            return orderMapper.selectList(queryWrapper);
        } catch (Exception e) {
            System.err.println("查询用户订单出错: " + e.getMessage());
            return null;
        }
    }

    @Override
    public int insert(Order order) {
        try {
            return orderMapper.insert(order);
        } catch (Exception e) {
            System.err.println("插入订单出错: " + e.getMessage());
            return 0;
        }
    }

    @Override
    public int update(Order order) {
        try {
            return orderMapper.updateById(order);
        } catch (Exception e) {
            System.err.println("更新订单出错: " + e.getMessage());
            return 0;
        }
    }

    @Override
    public int delete(Integer orderId) {
        try {
            return orderMapper.deleteById(orderId);
        } catch (Exception e) {
            System.err.println("删除订单出错: " + e.getMessage());
            return 0;
        }
    }
}
